package com.bayss.core.service;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.grade.Grade;
import com.bayss.core.entity.grade.GradeExample;
import com.bayss.core.entity.grade.mapper.GradeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by LDL on 2020/3/7.
 */
@Service
public class GradeService {
    @Autowired
    private GradeMapper gradeMapper;

    public List<Grade> list() {
        GradeExample example = new GradeExample();
        example.createCriteria().andStatusEqualTo(1);
        return gradeMapper.selectByExample(example);
    }

    /**
     * 删除
     */
    public ResultMsg delete(Integer id) {
        Grade gradeDB = gradeMapper.selectByPrimaryKey(id);
        if (gradeDB.getItemType().intValue()==1) {
            return ResultMsg.fail(-1, "该评分项是系统默认项，不允许修改");
        }

        gradeMapper.deleteByPrimaryKey(id);
        return ResultMsg.success();
    }

    public ResultMsg saveGrade(Grade grade) {
        GradeExample example = new GradeExample();
        example.createCriteria().andGradeItemEqualTo(grade.getGradeItem());
        List<Grade> list = gradeMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return ResultMsg.fail(-1, grade.getGradeItem() + "已存在");
        }

        grade.setCreateTime(new Date());
        grade.setStatus(1);
        gradeMapper.insertSelective(grade);
        return ResultMsg.success();
    }

    public ResultMsg updateGrade(Grade record) {
        Grade gradeDB = gradeMapper.selectByPrimaryKey(record.getId());
        if (gradeDB.getItemType().intValue() == 1) {
            return ResultMsg.fail(-1, "该评分项是系统默认项，不允许修改");
        }
        gradeMapper.updateByPrimaryKeySelective(record);
        return ResultMsg.success();
    }
}
